package com.spcmg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spcmg.entity.SpcRoom;
import com.spcmg.entity.SpcStation;
import com.spcmg.mapper.SpcStationMapper;
import com.spcmg.service.ISpcRoomService;
import com.spcmg.service.ISpcStationService;
import com.spcmg.utils.DateUtil;
import com.spcmg.utils.JsonResult;
import com.spcmg.vo.SpcRoomVO;
import com.spcmg.vo.SpcStationVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 局站，用于安置通信设备若干建筑物的集合；基站站点作为局站的一种，局站类型：2010004基站站点。 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-13
 */
@Service
public class SpcStationServiceImpl extends ServiceImpl<SpcStationMapper, SpcStation> implements ISpcStationService {

    @Resource
    private SpcStationMapper mapper;

    @Resource
    private ISpcRoomService spcRoomService;

    @Override
    public SpcStation selectByChinaName(String chinaName) {
        QueryWrapper<SpcStation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CHINA_NAME", chinaName);
        SpcStation entity = mapper.selectOne(queryWrapper);
        return entity;
    }

    @Override
    public Long selectIdByChinaName(String chinaName) {
        return mapper.selectIdByChinaName(chinaName);
    }

    @Override
    public JsonResult selectInfoByStationId(Long stationId) throws Exception {
        // 查询局站
        SpcStation entity = this.getById(stationId);
        SpcStationVO view = null;
        if (entity != null) {
            view = new SpcStationVO();
            BeanUtils.copyProperties(view, entity);
            // 查询机房信息
            QueryWrapper<SpcRoom> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("STATION_ID", stationId);
            List<SpcRoom> roomEntityList = spcRoomService.list(queryWrapper);
            List<SpcRoomVO> roomVOList = new ArrayList<>();
            for (SpcRoom roomEntity : roomEntityList) {
                SpcRoomVO roomView = new SpcRoomVO();
                BeanUtils.copyProperties(roomView, roomEntity);
                roomView.setStrStartUseDate(DateUtil.format(roomView.getStartUseDate(), "yyyy-MM-dd HH:mm:ss"));
                roomVOList.add(roomView);
            }
            // 设置机房信息
            view.setRoomList(roomVOList);
        }
        return JsonResult.success()
                .data(view)
                .build();
    }
}
